package com.jpf.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jpf.demo.Goods;
import com.jpf.service.GoodsService;
@WebServlet("/AlterGoods.do")
public class AlterGoods extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		GoodsService goodsService = new GoodsService();
		int gid =Integer.parseInt(request.getParameter("gid"));
		Goods goods = goodsService.queryGoods(gid);
		
		
		if (goods != null)
		request.getSession().setAttribute("goods", goods);
		request.getRequestDispatcher("admin/content.jsp").forward(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		String gid = request.getParameter("gid");
		String cid = request.getParameter("cid");
		String gname = request.getParameter("gname");
		String brand = request.getParameter("brand");
		String gimage = request.getParameter("gimage");
		String content = request.getParameter("content");
		String shelftime = request.getParameter("shelftime");
		String gprice = request.getParameter("gprice");
		String gstock = request.getParameter("gstock");
		System.out.println(gid);
		if(cid.equals("")||gname.equals("")||brand.equals("")||content.equals("")||shelftime.equals("")||gprice.equals("")||gstock.equals("")) {
			response.sendRedirect("admin/error.jsp");
		}else {
			Goods goods = new Goods(Integer.parseInt(gid),cid, gname, brand, gimage, content, shelftime, Double.parseDouble(gprice), Integer.parseInt(gstock));
			out.println(cid);out.println(gname);out.println(brand);out.println(content);out.println(shelftime);out.println(gprice);out.println(gstock);
			GoodsService goodsService = new GoodsService(); 
			if(goodsService.updateGoods(goods))
				response.sendRedirect("QueryGoods.do");
			else response.sendRedirect("admin/error.jsp");
		}
		
	}

}
